import wx from 'weixin-js-sdk'
import { getJsTicket, lottery } from '@/api/common';

export function WxShare() {
    // let url = encodeURIComponent(window.location.href.split('#')[0]);
    let url = "http://www.whxgjt.com/dfh2/#/";
    if (location.href.split("?code").length > 1) {
        location.href = url;
    }
    getJsTicket(url).then(function (res) {
        let data = res.data;
        wx.config({
            debug: false,
            appId: data.appId, // 和获取Ticke的必须一样------必填，公众号的唯一标识
            timestamp: data.timestamp, // 必填，生成签名的时间戳
            nonceStr: data.nonceStr, // 必填，生成签名的随机串
            signature: data.signature,// 必填，签名，见附录1
            //需要分享的列表项:发送给朋友，分享到朋友圈，分享到QQ，分享到QQ空间
            jsApiList: [
                'onMenuShareTimeline',
                'onMenuShareAppMessage',
                // 'updateAppMessageShareData',
                // 'updateTimelineShareData'
            ]
        });

        let share_config = {
            imgUrl: "http://qiniu.whxgjt.com/dfh_log.png",
            title: '有奖问答 | 东方小红月饼工厂开张啦！',
            desc: '做月饼赢取精美礼品',
            link: url,
            success: async () => {
                //分享成功后的回调函数
                // console.log("成功回调：");
                const openid = localStorage.getItem("openid");
                if (!openid) return;
                try {
                    const res = await lottery({
                        userId: openid,
                        lotteryType: 2,
                    });
                    const data = res.data;
                    if (data.errorCode === 0) {
                        const rows = data.data;
                        const isHaveAddress = rows.isHaveAddress;
                        const lotteryStatus = rows.lotteryStatus;
                        if (lotteryStatus == 1) {
                            alert("恭喜你，中奖了~");
                            if (isHaveAddress) {
                                location.href = `${location.origin}${location.pathname}#/drawAward?giftId=${rows.giftId}&isShare=1`;
                            } else {
                                location.href = `${location.origin}${location.pathname}#/takeAddress`;
                            }
                        } else if (lotteryStatus == 2) {
                            alert("你已经抽到过礼物啦~");
                            location.href = `${location.origin}${location.pathname}#/getRecord`;
                        } else if (lotteryStatus == 0) {
                            alert("很遗憾没有抽中礼物~");
                            location.href = `${location.origin}${location.pathname}#/`;
                        } else if (lotteryStatus == 3) {
                            alert("你来晚啦……礼物已经发放完啦~");
                            location.href = `${location.origin}${location.pathname}#/`;
                        } else {
                            alert("你来晚啦……礼物已经发放完啦~");
                            location.href = `${location.origin}${location.pathname}#/`;
                        }
                    }
                } catch (e) {
                    console.log(e);
                }
                // window.location.href = url;
            },
            cancel: function () {
                //用户取消分享执行的回调函数
                // alert('分享成功')
            }
        }
        wx.ready(function () {
            // wx.updateAppMessageShareData(share_config);//分享给好友
            // wx.updateTimelineShareData(share_config);//分享到朋友圈
            wx.onMenuShareTimeline(share_config);
            wx.onMenuShareAppMessage(share_config);
            // wx.onMenuShareQQ(share_config);//分享给手机QQ
            // wx.onMenuShareQZone(share_config);//分享给手机QQ
            //wx.chooseWXPay(pay_config); //微信支付
        });
        wx.error(function (res) {
            // config信息验证失败会执行error函数，如签名过期导致验证失败，具体错误信息可以打开config的debug模式查看，也可以在返回的res参数中查看，对于SPA可以在这里更新签名。
            // console.log("错误信息：", res);
        });
    })

}
